package com.zhangxiaofanfan.hotcode.fourth.third;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode080 {
    public static void main(String[] args) {
        HotCode080 hotCode = new HotCode080();
        String s = "eccbbbbdec";
        System.out.println(hotCode.partitionLabels(s));
    }

    public List<Integer> partitionLabels(String s) {
        Map<Character, int[]> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), new int[]{i, i});
            }
            map.get(s.charAt(i))[1] = i;
        }
        List<int[]> list = new ArrayList<>(map.values());
        list.sort(Comparator.comparingInt(nums -> nums[0]));
        List<Integer> result = new ArrayList<>();
        int left = list.getFirst()[0], right = list.getFirst()[1];
        for (int i = 1; i < list.size(); i++) {
            if (right < list.get(i)[0]) {
                result.add(right - left + 1);
                left = list.get(i)[0];
                right = list.get(i)[1];
            } else {
                right = Math.max(right, list.get(i)[1]);
            }
        }
        result.add(right - left + 1);
        return result;
    }

}
